/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
export class Appendable {
    constructor(str) {
        if (str == null) {
            this.str = null;
        }
        else if (typeof str !== 'string') {
            this.str = str.toString();
        }
        else {
            this.str = str;
        }
    }
    append(s) {
        if (s == null) {
            s = 'null';
        }
        if (typeof s !== 'string') {
            s = s.toString();
        }
        if (this.str == null) {
            this.str = s;
        }
        else {
            this.str += s;
        }
        return this;
    }
    appendWithStartAndEnd(s, start, end) {
        if (s == null) {
            s = 'null';
        }
        if (this.str == null) {
            this.str = s;
        }
        else {
            if (this.checkRange(start, end, s.length)) {
                this.str += s.substring(start, end);
            }
        }
        return this;
    }
    toString() {
        if (this.str == null) {
            this.str = 'null';
        }
        return this.str;
    }
    length() {
        if (this.str == null) {
            this.str = 'null';
        }
        return this.str.length;
    }
    setLength(newLength) {
        if (this.str == null) {
            this.str = 'null';
        }
        if (newLength < 0 || newLength >= this.str.length) {
            return;
        }
        this.str.substring(0, newLength);
    }
    checkRange(start, end, len) {
        if (start < 0 || start > end || end > len) {
            return false;
        }
        return true;
    }
}
